<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thanhdx
 * Date: 4/12/12
 * Time: 3:18 PM
 * To change this template use File | Settings | File Templates.
 */
class UploadController extends Controller
{
    /**
     * @param $file CUploadedFile
     */
    private function saveFileInfo($file,$path,$ctype = '')
    {
        $item = new Files();
        $item->size = $file->size;
        $item->file_type = $file->type;
        $item->content_type = $ctype;
        $item->path = $path;
        $item->upload_time = time();
        $item->user_id = Yii::app()->user->id;
        return $item->save();
    }

    public function actionImage(){
        Yii::import("ext.EAjaxUpload.qqFileUploader");

        $folder='upload/images/';// folder for uploaded files
        $allowedExtensions = array("jpg", "png","jpeg","gif");//array("jpg","jpeg","gif","exe","mov" and etc...
        $sizeLimit = 1 * 1024 * 1024;// maximum file size in bytes
        $uploader = new qqFileUploader($allowedExtensions,$sizeLimit);
        $result = $uploader->handleUpload($folder);

        //var_dump($result);die;

        if($result){
            $path = $folder.$result['filename'];
            $pathinfo = pathinfo($path);
            $ext = $pathinfo['extension'];

            $item = new Files();
            $item->file_type = 'image/'.$ext;
            $item->path = $path;
            $item->size = filesize($path);
            $item->upload_time = time();
            $item->user_id = Yii::app()->user->id;
            $item->content_type = $_GET['ctype'];
            if($item->save()){
                $result['item_id'] = $item->id;
                
                //
                Yii::import('application.extensions.image.Image');
                $image = new Image($path);
                $image->resize(250, 100);
                $thumb = '/upload/images/thumb/'.$result['filename'];
                $path_thumb = Yii::getPathOfAlias('webroot').$thumb;
                if(!is_file($path_thumb)){
                	$image->save($path_thumb);
                }
            }
        }

        $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        echo $return;
    }

    public function actionVideo(){
        Yii::import("ext.EAjaxUpload.qqFileUploader");

        $folder='upload/video/';// folder for uploaded files
        $allowedExtensions = array("flv", "mp4","avi");//array("jpg","jpeg","gif","exe","mov" and etc...
        $sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
        $uploader = new qqFileUploader($allowedExtensions,$sizeLimit);
        $result = $uploader->handleUpload($folder);

        //var_dump($result);die;

        if($result){
            $path = $folder.$result['filename'];
            $pathinfo = pathinfo($path);
            $ext = $pathinfo['extension'];

            $item = new Files();
            $item->file_type = $ext;
            $item->path = $path;
            $item->size = filesize($path);
            $item->upload_time = time();
            $item->user_id = Yii::app()->user->id;
            $item->content_type = $_GET['ctype'];
            if($item->save()){
                $result['item_id'] = $item->id;
            }
        }

        $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        echo $return;
    }
    public function actionImage2()
    {
        $dir = Yii::getPathOfAlias('application.uploads.images');
        $uploaded = false;
        $model = new UploadImageForm();
        //var_dump($_POST);die;
        if (isset($_POST['UploadImageForm'])) {
            //echo '11';die;
            $model->attributes = $_POST['UploadImageForm'];
            $file = CUploadedFile::getInstance($model, 'file');
            if ($model->validate()) {
                $path = $dir . '/' . $file->getName();
                $uploaded = $file->saveAs($path);
                if ($uploaded) {
                    $this->saveFileInfo($file,$path);
                }
            }
        }
        $this->render('_form', array(
            'model' => $model,
        ));
    }
    public function actionUpload()
    {
        Yii::import("ext.EAjaxUpload.qqFileUploader");

        $folder='upload/';// folder for uploaded files
        $allowedExtensions = array("jpg","png","jpeg","gif");//array("jpg","jpeg","gif","exe","mov" and etc...
        $sizeLimit = 1 * 1024 * 1024;// maximum file size in bytes
        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
        $result = $uploader->handleUpload($folder);

         //var_dump($result);die;

        if($result){
            $path = $folder.$result['filename'];

            $item = new Files();
            $item->file_type = filetype($path);
            $item->path = $path;
            $item->size = filesize($path);
            $item->upload_time = time();
            $item->user_id = Yii::app()->user->id;
            $item->content_type = $_GET['ctype'];
            if($item->save()){
                $result['item_id'] = $item->id;
            }
        }

        $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        echo $return;// it's array
    }
}
